#include "c2queue.h"
 

struct Customer* head = NULL;
struct Customer* curr = NULL;
int listnum = 0;
 
 
 
 int main(){
    char line[200];
    char *opt;
    char *idnum;
    char *firstname;
    char *lastname;
    int id;
    int i = 0;
    FILE *datfile;
    int inbytes;
    struct Customer* temp;
    
    

      while(i == 0){
        fgets(line,sizeof(line), stdin);

        opt = strtok(line," \t\n");
       
        if(strcmp(opt,"a")==0 | strcmp(opt, "A")==0){
           
            idnum = strtok(NULL," \t\n");
            firstname = strtok(NULL," \t\n");
            lastname = strtok(NULL," \t\n");

            if(!idnum | !firstname | !lastname)
                printf("ERROR1\n");
            else{
            
                id = atoi(idnum);
            

            if(id < 0 | id > INT_MAX | (strlen(firstname) > 50) | (strlen(lastname) > 50))
                printf("ERROR2\n");
            else{
                temp = malloc(sizeof(temp));
                (*temp).id = id;
                strcpy((*temp).first,firstname);
                strcpy((*temp).last,lastname);
                if(addCust(temp) == 0)
                    printf("ERROR\n");
                else
                    printf("OK\n");
                }
            }
        }

        else if(strcmp(opt, "r")==0 | strcmp(opt, "R")==0){
            if(custCount() == 0)
                printf("ERROR4\n");
            else{
                temp = remCust();
                printf("ID: %d\nFirst: %s\nLast: %s\n",(*temp).id,(*temp).first,(*temp).last);
                free(&(*temp).id);
                free((*temp).first);
                free((*temp).last);
                free((*temp).next_ptr);

                
            }

        }

        else if(strcmp(opt, "c")==0 | strcmp(opt, "C")==0){
            printf("%d\n",custCount());
        }

        else if(strcmp(opt, "e")==0 | strcmp(opt, "E")==0){
            saveData();
            return EXIT_FAILURE;
        }

        else{
            printf("ERROR5\n");
        }
    }
}




int addCust(struct Customer* cust){
    if(head == NULL){
        head = cust;
        curr = cust;
        listnum++;
        printf("head id: %d, head name: %s %s\n",(*head).id,(*head).first,(*head).last);
    }
    else{
        
        (*curr).next_ptr = cust;
        curr = (*curr).next_ptr;
        listnum++;

        printf("head id: %d, head name: %s %s\n",(*head).id,(*head).first,(*head).last);
        printf("curr id: %d, curr name: %s %s\n",(*curr).id,(*curr).first,(*curr).last);
    }
    
    
    return 1;
}

struct Customer* remCust(){
    struct Customer* temp_head;
    temp_head = malloc(sizeof(struct Customer));
    temp_head = head;
    head = (*head).next_ptr;
    listnum--;
    return temp_head;
    
}

 int custCount(){
     return listnum;
 }

 int parseList(int id){
     return 0;    
 }

 int saveData(){
     return 0;
 }

 int loadData(){
     return 0;
 }
